package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.pop.sdk.common.util.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.appuser.entity.TbUserCash;
import org.jeecg.modules.appuser.entity.TbUserLuckdraw;
import org.jeecg.modules.appuser.entity.TbUserNotice;
import org.jeecg.modules.appuser.entity.TbUserWallet;
import org.jeecg.modules.appuser.mapper.TbUserCashMapper;
import org.jeecg.modules.appuser.service.ITbUserCashService;
import org.jeecg.modules.appuser.service.ITbUserNoticeService;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 用户体现审核表
 * @Author: jeecg-boot
 * @Date: 2020-05-18
 * @Version: V1.0
 */
@Service
public class TbUserCashServiceImpl extends ServiceImpl<TbUserCashMapper, TbUserCash> implements ITbUserCashService {
    @Autowired
    private ITbUserNoticeService tbUserNoticeService;

    @Autowired
    @Lazy(true)
    private ITbMnUserService tbMnUserService;

    @Override
    public Result<?> saveCashData(TbUserCash tbUserCash) {
        if (StringUtils.isEmpty(tbUserCash.getUserId()) || null == tbUserCash.getCashBalance()) {
            return Result.ok("参数错误");
        }
        tbUserCash.setState(3);
        tbUserCash.setApplyState(3);
        if (this.save(tbUserCash)) {
            addMsgNotice(tbUserCash);
            return Result.ok("提现申请成功");
        }
        return Result.ok("提现申请失败");
    }

    @Override
    public IPage<TbUserCash> getUserCashList(Page<TbUserCash> page, TbUserCash tbUserCash) {
        return page.setRecords(this.baseMapper.getTbUserCashDatas(page, tbUserCash));
    }


    //增加消息通知
    private void addMsgNotice(TbUserCash tbUserCash) {
        TbUserNotice tbUserNotice=new TbUserNotice();
        TbMnUser tbMnUser= tbMnUserService.getById(tbUserCash.getUserId());
        tbUserNotice.setNoticeContent(tbMnUser.getPhone()+":申请"+ tbUserCash.getCashBalance()+"提现完成。");
        tbUserNotice.setNoticeTitle(tbMnUser.getAccount()+ "提现申请完成！");
        tbUserNotice.setNoticeSendStatus("0");
        tbUserNotice.setUserId(tbUserCash.getUserId());
        tbUserNotice.setNoticeType("1");
        tbUserNoticeService.addUserNotice(tbUserNotice);
    }
}
